home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 July / CHIP 2006-07.2.iso / program / web_gelistirme / easyphp1-7_setup.exe / {app} / phpmyadmin / libraries / relation.lib.php < prev    next >
Encoding:
PHP Script  |  2003-09-07  |  27.1 KB  |  644 lines

  1. <?php
  2. /* $Id: relation.lib.php,v 1.44 2003/08/17 23:37:50 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Set of functions used with the relation and pdf feature
  7.  */
  8.  
  9.  
  10. if (!defined('PMA_RELATION_LIB_INCLUDED')){
  11.     define('PMA_RELATION_LIB_INCLUDED', 1);
  12.  
  13.     /**
  14.      * Executes a query as controluser if possible, otherwise as normal user
  15.      *
  16.      * @param   string   the query to execute
  17.      * @param   boolean  whether to display SQL error messages or not
  18.      *
  19.      * @return  integer  the result id
  20.      *
  21.      * @global  string   the URL of the page to show in case of error
  22.      * @global  string   the name of db to come back to
  23.      * @global  integer  the ressource id of DB connect as controluser
  24.      * @global  array    configuration infos about the relations stuff
  25.      *
  26.      * @access  public
  27.      *
  28.      * @author  Mike Beck <mikebeck@users.sourceforge.net>
  29.      */
  30.      function PMA_query_as_cu($sql, $show_error = TRUE) {
  31.         global $err_url_0, $db, $dbh, $cfgRelation;
  32.  
  33.         if (isset($dbh)) {
  34.             PMA_mysql_select_db($cfgRelation['db'], $dbh);
  35.             $result = @PMA_mysql_query($sql, $dbh);
  36.             if (!$result && $show_error == TRUE) {
  37.                 PMA_mysqlDie(mysql_error($dbh), $sql, '', $err_url_0);
  38.             }
  39.             PMA_mysql_select_db($db, $dbh);
  40.         } else {
  41.             PMA_mysql_select_db($cfgRelation['db']);
  42.             $result = @PMA_mysql_query($sql);
  43.             if ($result && $show_error == TRUE) {
  44.                 PMA_mysqlDie('', $sql, '', $err_url_0);
  45.             }
  46.             PMA_mysql_select_db($db);
  47.         } // end if... else...
  48.  
  49.         if ($result) {
  50.             return $result;
  51.         } else {
  52.             return FALSE;
  53.         }
  54.      } // end of the "PMA_query_as_cu()" function
  55.  
  56.  
  57.     /**
  58.      * Defines the relation parameters for the current user
  59.      * just a copy of the functions used for relations ;-)
  60.      * but added some stuff to check what will work
  61.      *
  62.      * @param   boolean  whether to check validity of settings or not
  63.      *
  64.      * @return  array    the relation parameters for the current user
  65.      *
  66.      * @global  array    the list of settings for servers
  67.      * @global  integer  the id of the current server
  68.      * @global  string   the URL of the page to show in case of error
  69.      * @global  string   the name of the current db
  70.      * @global  string   the name of the current table
  71.      * @global  array    configuration infos about the relations stuff
  72.      *
  73.      * @access  public
  74.      *
  75.      * @author  Mike Beck <mikebeck@users.sourceforge.net>
  76.      */
  77.     function PMA_getRelationsParam($verbose = FALSE)
  78.     {
  79.         global $cfg, $server, $err_url_0, $db, $table;
  80.         global $cfgRelation;
  81.  
  82.         $cfgRelation                = array();
  83.         $cfgRelation['relwork']     = FALSE;
  84.         $cfgRelation['displaywork'] = FALSE;
  85.         $cfgRelation['bookmarkwork']= FALSE;
  86.         $cfgRelation['pdfwork']     = FALSE;
  87.         $cfgRelation['commwork']    = FALSE;
  88.         $cfgRelation['mimework']    = FALSE;
  89.         $cfgRelation['historywork'] = FALSE;
  90.         $cfgRelation['allworks']    = FALSE;
  91.  
  92.         // No server selected -> no bookmark table
  93.         // we return the array with the FALSEs in it,
  94.         // to avoid some 'Unitialized string offset' errors later
  95.         if ($server == 0
  96.            || empty($cfg['Server'])
  97.            || empty($cfg['Server']['pmadb'])) {
  98.             if ($verbose == TRUE) {
  99.                 echo 'PMA Database ... '
  100.                      . '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font>'
  101.                      . '[ <a href="Documentation.html#pmadb">' . $GLOBALS['strDocu'] . '</a> ]<br />' . "\n"
  102.                      . $GLOBALS['strGeneralRelationFeat']
  103.                      . ' <font color="green">' . $GLOBALS['strDisabled'] . '</font>' . "\n";
  104.             }
  105.             return $cfgRelation;
  106.         }
  107.  
  108.         $cfgRelation['user']  = $cfg['Server']['user'];
  109.         $cfgRelation['db']    = $cfg['Server']['pmadb'];
  110.  
  111.         //  Now I just check if all tables that i need are present so I can for
  112.         //  example enable relations but not pdf...
  113.         //  I was thinking of checking if they have all required columns but I
  114.         //  fear it might be too slow
  115.         // PMA_mysql_select_db($cfgRelation['db']);
  116.  
  117.         $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($cfgRelation['db']);
  118.         $tab_rs    = PMA_query_as_cu($tab_query, FALSE);
  119.  
  120.         while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
  121.             if ($curr_table[0] == $cfg['Server']['bookmarktable']) {
  122.                 $cfgRelation['bookmark']        = $curr_table[0];
  123.             } else if ($curr_table[0] == $cfg['Server']['relation']) {
  124.                 $cfgRelation['relation']        = $curr_table[0];
  125.             } else if ($curr_table[0] == $cfg['Server']['table_info']) {
  126.                 $cfgRelation['table_info']      = $curr_table[0];
  127.             } else if ($curr_table[0] == $cfg['Server']['table_coords']) {
  128.                 $cfgRelation['table_coords']    = $curr_table[0];
  129.             } else if ($curr_table[0] == $cfg['Server']['column_info']) {
  130.                 $cfgRelation['column_info'] = $curr_table[0];
  131.             } else if ($curr_table[0] == $cfg['Server']['pdf_pages']) {
  132.                 $cfgRelation['pdf_pages']       = $curr_table[0];
  133.             } else if ($curr_table[0] == $cfg['Server']['history']) {
  134.                 $cfgRelation['history'] = $curr_table[0];
  135.             }
  136.         } // end while
  137.         if (isset($cfgRelation['relation'])) {
  138.             $cfgRelation['relwork']         = TRUE;
  139.             if (isset($cfgRelation['table_info'])) {
  140.                     $cfgRelation['displaywork'] = TRUE;
  141.             }
  142.         }
  143.         if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
  144.             $cfgRelation['pdfwork']     = TRUE;
  145.         }
  146.         if (isset($cfgRelation['column_info'])) {
  147.             $cfgRelation['commwork']    = TRUE;
  148.                
  149.             if ($cfg['Server']['verbose_check']) {
  150.                 $mime_query  = 'SHOW FIELDS FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
  151.                 $mime_rs     = PMA_query_as_cu($mime_query, FALSE);
  152.                     
  153.                 $mime_field_mimetype                = FALSE;
  154.                 $mime_field_transformation          = FALSE;
  155.                 $mime_field_transformation_options  = FALSE;
  156.                 while ($curr_mime_field = @PMA_mysql_fetch_array($mime_rs)) {
  157.                     if ($curr_mime_field[0] == 'mimetype') {
  158.                         $mime_field_mimetype               = TRUE;
  159.                     } else if ($curr_mime_field[0] == 'transformation') {
  160.                         $mime_field_transformation         = TRUE; 
  161.                     } else if ($curr_mime_field[0] == 'transformation_options') {
  162.                         $mime_field_transformation_options = TRUE; 
  163.                     }
  164.                 }
  165.                    
  166.                 if ($mime_field_mimetype == TRUE
  167.                     && $mime_field_transformation == TRUE
  168.                     && $mime_field_transformation_options == TRUE) {
  169.                     $cfgRelation['mimework'] = TRUE;
  170.                 }
  171.             } else {
  172.                 $cfgRelation['mimework'] = TRUE;
  173.             }
  174.         }
  175.         
  176.         if (isset($cfgRelation['history'])) {
  177.             $cfgRelation['historywork']     = TRUE;
  178.         }
  179.  
  180.         if (isset($cfgRelation['bookmark'])) {
  181.             $cfgRelation['bookmarkwork']     = TRUE;
  182.         }
  183.  
  184.         if ($cfgRelation['relwork'] == TRUE && $cfgRelation['displaywork'] == TRUE
  185.             && $cfgRelation['pdfwork'] == TRUE && $cfgRelation['commwork'] == TRUE
  186.             && $cfgRelation['mimework'] == TRUE && $cfgRelation['historywork'] == TRUE
  187.             && $cfgRelation['bookmarkwork'] == TRUE) {
  188.             $cfgRelation['allworks'] = TRUE;
  189.         }
  190.         if ($tab_rs) {
  191.             mysql_free_result($tab_rs);
  192.         } else {
  193.             $cfg['Server']['pmadb'] = FALSE;
  194.         }
  195.  
  196.         if ($verbose == TRUE) {
  197.             $shit     = '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font> [ <a href="Documentation.html#%s">' . $GLOBALS['strDocu'] . '</a> ]';
  198.             $hit      = '<font color="green"><b>' . $GLOBALS['strOK'] . '</b></font>';
  199.             $enabled  = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
  200.             $disabled = '<font color="red">'   . $GLOBALS['strDisabled'] . '</font>';
  201.  
  202.             echo '<table>' . "\n";
  203.             echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'pmadb\'] ... </th><td align="right">'
  204.                  . (($cfg['Server']['pmadb'] == FALSE) ? sprintf($shit, 'pmadb') : $hit)
  205.                  . '</td></tr>' . "\n";
  206.             echo '    <tr><td> </td></tr>' . "\n";
  207.  
  208.             echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'relation\'] ... </th><td align="right">'
  209.                  . ((isset($cfgRelation['relation'])) ? $hit : sprintf($shit, 'relation'))
  210.                  . '</td></tr>' . "\n";
  211.             echo '    <tr><td colspan=2 align="center">'. $GLOBALS['strGeneralRelationFeat'] . ': '
  212.                  . (($cfgRelation['relwork'] == TRUE) ? $enabled :  $disabled)
  213.                  . '</td></tr>' . "\n";
  214.             echo '    <tr><td> </td></tr>' . "\n";
  215.  
  216.             echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'table_info\']   ... </th><td align="right">'
  217.                  . (($cfgRelation['displaywork'] == FALSE) ? sprintf($shit, 'table_info') : $hit)
  218.                  . '</td></tr>' . "\n";
  219.             echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strDisplayFeat'] . ': '
  220.                  . (($cfgRelation['displaywork'] == TRUE) ? $enabled : $disabled)
  221.                  . '</td></tr>' . "\n";
  222.             echo '    <tr><td> </td></tr>' . "\n";
  223.  
  224.             echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'table_coords\'] ... </th><td align="right">'
  225.                  . ((isset($cfgRelation['table_coords'])) ? $hit : sprintf($shit, 'table_coords'))
  226.                  . '</td></tr>' . "\n";
  227.             echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'pdf_pages\'] ... </th><td align="right">'
  228.                  . ((isset($cfgRelation['pdf_pages'])) ? $hit : sprintf($shit, 'table_coords'))
  229.                  . '</td></tr>' . "\n";
  230.             echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strCreatePdfFeat'] . ': '
  231.                  . (($cfgRelation['pdfwork'] == TRUE) ? $enabled : $disabled)
  232.                  . '</td></tr>' . "\n";
  233.             echo '    <tr><td> </td></tr>' . "\n";
  234.  
  235.             echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'column_info\'] ... </th><td align="right">'
  236.                  . ((isset($cfgRelation['column_info'])) ? $hit : sprintf($shit, 'col_com'))
  237.                  . '</td></tr>' . "\n";
  238.             echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strColComFeat'] . ': '
  239.                  . (($cfgRelation['commwork'] == TRUE) ? $enabled : $disabled)
  240.                  . '</td></tr>' . "\n";
  241.             echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strBookmarkQuery'] . ': '
  242.                  . (($cfgRelation['bookmarkwork'] == TRUE) ? $enabled : $disabled)
  243.                  . '</td></tr>' . "\n";
  244.             echo '    <tr><th align="left">MIME ...</th><td align="right">'
  245.                  . (($cfgRelation['mimework'] == TRUE) ? $hit : sprintf($shit, 'col_com')) 
  246.                  . '</td></tr>' . "\n";
  247.  
  248.                  if (($cfgRelation['commwork'] == TRUE) && ($cfgRelation['mimework'] != TRUE)) {
  249.                      echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
  250.                  }
  251.  
  252. //                 . '<br />(MIME: ' . (($cfgRelation['mimework'] == TRUE) ? $enabled : $disabled) . ')'
  253.  
  254.             echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'history\'] ... </th><td align="right">'
  255.                  . ((isset($cfgRelation['history'])) ? $hit : sprintf($shit, 'history'))
  256.                  . '</td></tr>' . "\n";
  257.             echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strQuerySQLHistory'] . ': '
  258.                  . (($cfgRelation['historywork'] == TRUE) ? $enabled : $disabled)
  259.                  . '</td></tr>' . "\n";
  260.  
  261.             echo '</table>' . "\n";
  262.         } // end if ($verbose == TRUE) {
  263.  
  264.         return $cfgRelation;
  265.     } // end of the 'PMA_getRelationsParam()' function
  266.  
  267.  
  268.     /**
  269.      * Gets all Relations to foreign tables for a given table or
  270.      * optionally a given column in a table
  271.      *
  272.      * @param   string   the name of the db to check for
  273.      * @param   string   the name of the table to check for
  274.      * @param   string   the name of the column to check for
  275.      * @param   string   the source for foreign key information
  276.      *
  277.      * @return  array    db,table,column
  278.      *
  279.      * @global  array    the list of relations settings
  280.      * @global  string   the URL of the page to show in case of error
  281.      *
  282.      * @access  public
  283.      *
  284.      * @author  Mike Beck <mikebeck@users.sourceforge.net> and Marc Delisle
  285.      */
  286.     function PMA_getForeigners($db, $table, $column = '', $source = 'both') {
  287.         global $cfgRelation, $err_url_0;
  288.  
  289.         if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
  290.             $rel_query     = 'SELECT master_field, foreign_db, foreign_table, foreign_field'
  291.                            . ' FROM ' . PMA_backquote($cfgRelation['relation'])
  292.                            . ' WHERE master_db =  \'' . PMA_sqlAddslashes($db) . '\' '
  293.                            . ' AND   master_table = \'' . PMA_sqlAddslashes($table) . '\' ';
  294.             if (!empty($column)) {
  295.                 $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\'';
  296.             }
  297.             $relations     = PMA_query_as_cu($rel_query);
  298.             $i = 0;
  299.             while ($relrow = @PMA_mysql_fetch_array($relations)) {
  300.                 $field                            = $relrow['master_field'];
  301.                 $foreign[$field]['foreign_db']    = $relrow['foreign_db'];
  302.                 $foreign[$field]['foreign_table'] = $relrow['foreign_table'];
  303.                 $foreign[$field]['foreign_field'] = $relrow['foreign_field'];
  304.                 $i++;
  305.             } // end while
  306.         }
  307.  
  308.         if (PMA_MYSQL_INT_VERSION >= 32320 && ($source == 'both' || $source == 'innodb') && !empty($table)) {
  309.             $show_create_table_query = 'SHOW CREATE TABLE ' 
  310.                 . PMA_backquote($db) . '.' . PMA_backquote($table);
  311.             $show_create_table_res = PMA_mysql_query($show_create_table_query);
  312.             list(,$show_create_table) = PMA_mysql_fetch_row($show_create_table_res);
  313.  
  314.             $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  315.             while (list(,$one_key) = each ($analyzed_sql[0]['foreign_keys'])) {
  316.  
  317.             // the analyzer may return more than one column name in the
  318.             // index list or the ref_index_list
  319.                 while (list($i,$field) = each($one_key['index_list'])) {
  320.  
  321.             // If a foreign key is defined in the 'internal' source (pmadb)
  322.             // and in 'innodb', we won't get it twice if $source='both'
  323.             // because we use $field as key
  324.  
  325.                     if (isset($one_key['ref_db_name'])) {
  326.                         $foreign[$field]['foreign_db']    = $one_key['ref_db_name'];
  327.                     } else {
  328.                         $foreign[$field]['foreign_db']    = $db;
  329.                     }
  330.                     $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
  331.                     $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
  332.                 }
  333.             }
  334.         }      
  335.  
  336.         if (isset($foreign) && is_array($foreign)) {
  337.            return $foreign;
  338.         } else {
  339.            return FALSE;
  340.         }
  341.     } // end of the 'PMA_getForeigners()' function
  342.  
  343.  
  344.     /**
  345.      * Gets the display field of a table
  346.      *
  347.      * @param   string   the name of the db to check for
  348.      * @param   string   the name of the table to check for
  349.      *
  350.      * @return  string   field name
  351.      *
  352.      * @global  array    the list of relations settings
  353.      *
  354.      * @access  public
  355.      *
  356.      * @author  Mike Beck <mikebeck@users.sourceforge.net>
  357.      */
  358.     function PMA_getDisplayField($db, $table) {
  359.         global $cfgRelation;
  360.  
  361.         $disp_query = 'SELECT display_field FROM ' . PMA_backquote($cfgRelation['table_info'])
  362.                     . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  363.                     . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
  364.  
  365.         $disp_res   = PMA_query_as_cu($disp_query);
  366.         $row        = ($disp_res ? PMA_mysql_fetch_array($disp_res) : '');
  367.         if (isset($row['display_field'])) {
  368.             return $row['display_field'];
  369.         } else {
  370.             return FALSE;
  371.         }
  372.     } // end of the 'PMA_getDisplayField()' function
  373.  
  374.  
  375.     /**
  376.      * Gets the comments for all rows of a table
  377.      *
  378.      * @param   string   the name of the db to check for
  379.      * @param   string   the name of the table to check for
  380.      *
  381.      * @return  array    [field_name] = comment
  382.      *
  383.      * @global  array    the list of relations settings
  384.      *
  385.      * @access  public
  386.      *
  387.      * @author  Mike Beck <mikebeck@users.sourceforge.net>
  388.      */
  389.     function PMA_getComments($db, $table = '') {
  390.         global $cfgRelation;
  391.  
  392.         if ($table != '') {
  393.             $com_qry  = 'SELECT column_name, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info'])
  394.                       . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  395.                       . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
  396.             $com_rs   = PMA_query_as_cu($com_qry);
  397.         } else {
  398.             $com_qry  = 'SELECT comment FROM ' . PMA_backquote($cfgRelation['column_info'])
  399.                       . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  400.                       . ' AND table_name = \'\''
  401.                       . ' AND column_name = \'(db_comment)\'';
  402.             $com_rs   = PMA_query_as_cu($com_qry);
  403.         }
  404.  
  405.         $i = 0;
  406.         while ($row = @PMA_mysql_fetch_array($com_rs)) {
  407.             $i++;
  408.             $col           = ($table != '' ? $row['column_name'] : $i);
  409.             
  410.             if (strlen($row['comment']) > 0) {
  411.                 $comment[$col] = $row['comment'];
  412.             }
  413.  
  414.         } // end while
  415.  
  416.         if (isset($comment) && is_array($comment)) {
  417.             return $comment;
  418.          } else {
  419.             return FALSE;
  420.          }
  421.      } // end of the 'PMA_getComments()' function
  422.  
  423.   /**
  424.     * Adds/removes slashes if required
  425.     *
  426.     * @param   string  the string to slash
  427.     *
  428.     * @return  string  the slashed string
  429.     *
  430.     * @access  public
  431.     */
  432.     function PMA_handleSlashes($val) {
  433.       return (get_magic_quotes_gpc() ? str_replace('\\"', '"', $val) : PMA_sqlAddslashes($val));
  434.     } // end of the "PMA_handleSlashes()" function
  435.  
  436.   /**
  437.     * Set a single comment to a certain value.
  438.     *
  439.     * @param   string   the name of the db
  440.     * @param   string   the name of the table
  441.     * @param   string   the name of the column
  442.     * @param   string   the value of the column
  443.     * @param   string   (optional) if a column is renamed, this is the name of the former key which will get deleted
  444.     *
  445.     * @return  boolean  true, if comment-query was made.
  446.     *
  447.     * @global  array    the list of relations settings
  448.     *
  449.     * @access  public
  450.     */
  451.     function PMA_setComment($db, $table, $key, $value, $removekey = '') {
  452.         global $cfgRelation;
  453.  
  454.         if ($removekey != '' AND $removekey != $key) {
  455.             $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
  456.                         . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  457.                         . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  458.                         . ' AND column_name = \'' . PMA_sqlAddslashes($removekey) . '\'';
  459.             $rmv_rs    = PMA_query_as_cu($remove_query);
  460.             unset($rmv_query);
  461.         }
  462.  
  463.         $test_qry  = 'SELECT ' . PMA_backquote('comment') . ', mimetype, transformation, transformation_options FROM ' . PMA_backquote($cfgRelation['column_info'])
  464.                     . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  465.                     . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  466.                     . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
  467.         $test_rs   = PMA_query_as_cu($test_qry);
  468.  
  469.         if ($test_rs && mysql_num_rows($test_rs) > 0) {
  470.             $row = @PMA_mysql_fetch_array($test_rs);
  471.  
  472.             if (strlen($value) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) {
  473.                 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
  474.                        . ' SET ' . PMA_backquote('comment') . ' = \'' . PMA_sqlAddslashes($value) . '\''
  475.                        . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  476.                        . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  477.                        . ' AND column_name = \'' . PMA_sqlAddSlashes($key) . '\'';
  478.             } else {
  479.                 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
  480.                        . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  481.                        . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  482.                        . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
  483.             }
  484.         } else if (strlen($value) > 0) {
  485.             $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_info'])
  486.                        . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
  487.                        . ' VALUES('
  488.                        . '\'' . PMA_sqlAddslashes($db) . '\','
  489.                        . '\'' . PMA_sqlAddslashes($table) . '\','
  490.                        . '\'' . PMA_sqlAddslashes($key) . '\','
  491.                        . '\'' . PMA_sqlAddslashes($value) . '\')';
  492.         }
  493.  
  494.         if (isset($upd_query)){
  495.             $upd_rs    = PMA_query_as_cu($upd_query);
  496.             unset($upd_query);
  497.             return true;
  498.         } else {
  499.             return false;
  500.         }
  501.     } // end of 'PMA_setComment()' function
  502.  
  503. /**
  504.     * Set a SQL history entry
  505.     *
  506.     * @param   string   the name of the db
  507.     * @param   string   the name of the table
  508.     * @param   string   the username
  509.     * @param   string   the sql query
  510.     *
  511.     * @return  boolean  true
  512.     *
  513.     * @access  public
  514.     */
  515.     function PMA_setHistory($db, $table, $username, $sqlquery) {
  516.         global $cfgRelation;
  517.         
  518.         $hist_rs    = PMA_query_as_cu('INSERT INTO ' . PMA_backquote($cfgRelation['history']) . ' ('
  519.                     . PMA_backquote('username') . ','
  520.                     . PMA_backquote('db') . ','
  521.                     . PMA_backquote('table') . ','
  522.                     . PMA_backquote('timevalue') . ','
  523.                     . PMA_backquote('sqlquery')
  524.                     . ') VALUES ('
  525.                     . '\'' . PMA_sqlAddslashes($username) . '\','
  526.                     . '\'' . PMA_sqlAddslashes($db) . '\','
  527.                     . '\'' . PMA_sqlAddslashes($table) . '\','
  528.                     . 'NOW(),'
  529.                     . '\'' . PMA_sqlAddslashes($sqlquery) . '\')');
  530.         return true;
  531.     } // end of 'PMA_setHistory()' function
  532.  
  533. /**
  534.     * Gets a SQL history entry
  535.     *
  536.     * @param   string   the username
  537.     *
  538.     * @return  array    list of history items
  539.     *
  540.     * @access  public
  541.     */
  542.     function PMA_getHistory($username) {
  543.         global $cfgRelation;
  544.         
  545.         $hist_rs    = PMA_query_as_cu('SELECT '
  546.                         . PMA_backquote('db') . ','
  547.                         . PMA_backquote('table') . ','
  548.                         . PMA_backquote('sqlquery')
  549.                         . ' FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE username = \'' . PMA_sqlAddslashes($username) . '\' ORDER BY id DESC');
  550.         
  551.         $history = array();
  552.         
  553.         while ($row = @PMA_mysql_fetch_array($hist_rs)) {
  554.             $history[] = $row;
  555.         }
  556.  
  557.         return $history;
  558.         
  559.     } // end of 'PMA_getHistory()' function
  560.  
  561.     /**
  562.     * Set a SQL history entry
  563.     *
  564.     * @param   string   the name of the db
  565.     * @param   string   the name of the table
  566.     * @param   string   the username
  567.     * @param   string   the sql query
  568.     *
  569.     * @return  boolean  true
  570.     *
  571.     * @access  public
  572.     */
  573.     function PMA_purgeHistory($username) {
  574.         global $cfgRelation, $cfg;
  575.  
  576.         $purge_rs = PMA_query_as_cu('SELECT timevalue FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE username = \'' . PMA_sqlAddSlashes($username) . '\' ORDER BY timevalue DESC LIMIT ' . $cfg['QueryHistoryMax'] . ', 1');
  577.         $i = 0;
  578.         $row = @PMA_mysql_fetch_array($purge_rs);
  579.         
  580.         if (is_array($row) && isset($row[0]) && $row[0] > 0) {
  581.             $maxtime = $row[0];
  582.             $remove_rs = PMA_query_as_cu('DELETE FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE timevalue <= ' . $maxtime);
  583.         }
  584.  
  585.         return true;
  586.     } // end of 'PMA_purgeHistory()' function
  587.     
  588.     /**
  589.     * Outputs dropdown with values of foreign fields
  590.     *
  591.     * @param   string   the query of the foreign keys
  592.     * @param   string   the foreign field
  593.     * @param   string   the foreign field to display
  594.     * @param   string   the current data of the dropdown
  595.     *
  596.     * @return  string   the <option value=""><option>s
  597.     *
  598.     * @access  public
  599.     */
  600.     function PMA_foreignDropdown($disp, $foreign_field, $foreign_display, $data, $max = 100) {
  601.         global $cfg;
  602.         
  603.         $ret = '<option value=""></option>' . "\n";
  604.  
  605.         $reloptions = array('content-id' => array(), 'id-content' => array());
  606.         while ($relrow = @PMA_mysql_fetch_array($disp)) {
  607.             $key   = $relrow[$foreign_field];
  608.             if (strlen($relrow[$foreign_display]) <= $cfg['LimitChars']) {
  609.                 $value  = (($foreign_display != FALSE) ? htmlspecialchars($relrow[$foreign_display]) : '');
  610.                 $vtitle = '';
  611.             } else {
  612.                 $vtitle = htmlspecialchars($relrow[$foreign_display]);
  613.                 $value  = (($foreign_display != FALSE) ? htmlspecialchars(substr($vtitle, 0, $cfg['LimitChars']) . '...') : '');
  614.             }
  615.             
  616.             $reloption = '<option value="' . htmlspecialchars($key) . '"';
  617.             if ($vtitle != '') {
  618.                 $reloption .= ' title="' . $vtitle . '"';
  619.             }
  620.             
  621.             if ($key == $data) {
  622.                $reloption .= ' selected="selected"';
  623.             } // end if
  624.  
  625.             $reloptions['id-content'][] = $reloption . '>' . $value . ' - ' . htmlspecialchars($key) .  '</option>' . "\n";
  626.             $reloptions['content-id'][] = $reloption . '>' . htmlspecialchars($key) .  ' - ' . $value . '</option>' . "\n";
  627.         } // end while
  628.         
  629.         if ($max == -1 || count($reloptions['content-id']) < $max) {
  630.             $ret .= implode('', $reloptions['content-id']);
  631.             if (count($reloptions['content-id']) > 0) {
  632.                 $ret .= '<option value=""></option>' . "\n";
  633.                 $ret .= '<option value=""></option>' . "\n";
  634.             }
  635.         }
  636.  
  637.         $ret .= implode('', $reloptions['id-content']);
  638.         
  639.         return $ret;
  640.     } // end of 'PMA_foreignDropdown()' function
  641.     
  642. } // $__PMA_RELATION_LIB__
  643. ?>
  644.